package cn.jasonone.jasonfx.factory;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import cn.jasonone.jasonfx.entity.JFXBaseController;
import cn.jasonone.jasonfx.exception.NotFoundInterceptorException;
import cn.jasonone.jasonfx.interceptor.PropertyInterceptor;
import cn.jasonone.jasonfx.interceptor.TextFieldInterceptor;
import cn.jasonone.jasonfx.util.ClassUtil;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleFloatProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleMapProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleSetProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Node;
import javafx.scene.control.TextField;

/**
 * 属性工厂
 * 
 * @author Jason
 * @date: 2020-01-25 14:09
 */
public class PropertyFactory {
	/**
	 * 控件拦值变更截器
	 */
	private static final Map<Class<? extends Node>, Class<? extends PropertyInterceptor<? extends Node>>> INTERCEPTORS = new HashMap<>();

	static {
		// 注册默认处理器
		register(TextField.class, TextFieldInterceptor.class);
	}

	/**
	 * 注册控件值变更拦截器
	 * 
	 * @param nodeType        控件对象
	 * @param interceptorType 拦截器类型
	 */
	public static void register(Class<? extends Node> nodeType,
			Class<? extends PropertyInterceptor<? extends Node>> interceptorType) {
		INTERCEPTORS.put(nodeType, interceptorType);
	}

	/**
	 * 根据类型创建属性对象
	 * 
	 * @param type
	 * @return
	 */
	public static Property<?> createProperty(Class<?> type) {
		if (Boolean.class.isAssignableFrom(type)) {
			return new SimpleBooleanProperty();
		} else if (Double.class.isAssignableFrom(type)) {
			return new SimpleDoubleProperty();
		} else if (Float.class.isAssignableFrom(type)) {
			return new SimpleFloatProperty();
		} else if (Integer.class.isAssignableFrom(type)) {
			return new SimpleIntegerProperty();
		} else if (List.class.isAssignableFrom(type)) {
			return new SimpleListProperty<>();
		} else if (Long.class.isAssignableFrom(type)) {
			return new SimpleLongProperty();
		} else if (Map.class.isAssignableFrom(type)) {
			return new SimpleMapProperty<>();
		} else if (Set.class.isAssignableFrom(type)) {
			return new SimpleSetProperty<>();
		} else if (String.class.isAssignableFrom(type)) {
			return new SimpleStringProperty();
		} else {
			return new SimpleObjectProperty<>();
		}
	}

	/**
	 * 根据类型创建属性对象
	 * 
	 * @param type
	 * @return
	 */
	public static PropertyInterceptor<?> createPropertyInterceptor(Object source, Field field,
			Node node) {
		for (Entry<Class<? extends Node>, Class<? extends PropertyInterceptor<? extends Node>>> entry : INTERCEPTORS
				.entrySet()) {
			if (entry.getKey().isInstance(node)) {
				return ClassUtil.newInstance(entry.getValue(), node, source, field);
			}
		}
		throw new NotFoundInterceptorException("未找到控件[" + node.getClass().getName() + "]的值更改拦截器");
	}
}
